package com.example.testcomposeproject.pages.testEt

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.example.testcomposeproject.widget.ChooseItem
import com.example.testcomposeproject.widget.InputOrChooseItem
import com.ioidea.base.PreviewPhoneSize.PreviewPhoneSize
import com.ioidea.base.theme.AppTheme
import com.ioidea.base.widget.LoadingDialog
import com.ioidea.base.widget.Toolbar
import kotlinx.coroutines.launch

/**
 * @author: hemin
 * @date: 2024/6/21
 * @Desc:
 */
@Preview(showBackground = true, device = PreviewPhoneSize)
@Composable
fun PreViewTestEtUI(modifier: Modifier = Modifier) {
    AppTheme {
        TestEtUI(modifier = modifier, uiState = TestEtContract.State(), onClickBack = {})

    }
}

/**
 * viewModel 用这个方法
 */
@Composable
fun TestEtScreen(modifier: Modifier = Modifier, viewModel: TestEtViewModel = hiltViewModel(), onClickBack:()->Unit) {
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    TestEtUI(modifier,uiState,onClickBack, onEtLamppostValueChange = {
        viewModel.onEtLamppostValueChange(it)

    })

    LoadingDialog(
        message = uiState.isShowDialog.text,
        showDialog = uiState.isShowDialog.loading
    )
}

/**
 * 真正的UI元素
 */
@Composable
fun TestEtUI(modifier: Modifier = Modifier,
             uiState:TestEtContract.State,
             onClickBack: () -> Unit,
                onEtLamppostValueChange: (String?) -> Unit = { _ -> }
) {
    Column(modifier = modifier.fillMaxSize()) {
        Toolbar(title = "TestEt screen", showRightIcon1 = false, onClickBack = onClickBack)


        InputOrChooseItem(
            title = "灯杆编号",
            content = uiState.lamppostNo ?: "",
            onValueChange = { lamppostName ->
                onEtLamppostValueChange(lamppostName)

            },
            onClickChoose = {


            })
        HorizontalDivider()

        ChooseItem(
            title = "灯杆类型",
            content = uiState.lamppostTypeName,
            hintText = "请选择灯杆类型",
            clickAble = !uiState.lamppostAlreadyExist
        ) {

//            onClickChooseLamppostModel()
        }
    }
}